package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.ydkj.a37e_mall.Classes.Applicatiom.MyApplication;
import com.ydkj.a37e_mall.Classes.Model.Request.ModifyPasswordReq;
import com.ydkj.a37e_mall.Classes.Model.Request.VerificateCodeReq;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.Async_http_get;
import com.ydkj.a37e_mall.Resource.Utils.CommonUtil;
import com.ydkj.a37e_mall.Resource.Utils.Const;
import com.ydkj.a37e_mall.Resource.Utils.Rsa;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;
import com.ydkj.a37e_mall.Resource.View.CustomProgressDialog;
import com.ydkj.a37e_mall.Resource.View.StatusBarUtil;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;


/**
 * Created by hong on 2016/3/16.
 * <p>
 * 修改密码
 */
public class Modify_Password extends AppCompatActivity {

    private Activity mActivity;
    private TextView tv_title;
    private EditText et_phone_number, et_verificate_code, et_password, et_oldpassword;
    private TextView tv_send_verificate_code;
    private TextView tv_verificate_message, tv_complete;
    private String secret;
    private ImageView img_back;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private MyApplication myApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forget_password);

        mActivity = this;
        init();
//        initWindow();//沉浸式状态栏
    }

    private void initWindow() {
        StatusBarUtil.setColor(mActivity, getResources().getColor(R.color.A333333), 0);
    }

    private void init() {
        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(mActivity);
        myApplication = MyApplication.getInstance();
        img_back = (ImageView) mActivity.findViewById(R.id.img_back);
        tv_title = (TextView) mActivity.findViewById(R.id.tv_title);
        tv_send_verificate_code = (TextView) mActivity.findViewById(R.id.tv_send_verificate_code);
        tv_verificate_message = (TextView) mActivity.findViewById(R.id.tv_verificate_message);
        et_phone_number = (EditText) mActivity.findViewById(R.id.et_phone_number);
        et_verificate_code = (EditText) mActivity.findViewById(R.id.et_verificate_code);
        et_password = (EditText) mActivity.findViewById(R.id.et_password);
        et_oldpassword = (EditText) mActivity.findViewById(R.id.et_oldpassword);
        et_oldpassword.setVisibility(View.VISIBLE);
        tv_complete = (TextView) mActivity.findViewById(R.id.tv_complete);

        tv_title.setText("修改密码");

        tv_complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_phone_number.getText().toString().equals("")) {
                    CommonUtil.showToast(mActivity, "请输入手机号");
                    return;
                }
                if (et_verificate_code.getText().toString().equals("")) {
                    CommonUtil.showToast(mActivity, "请输入验证码");
                    return;
                }
                if (et_password.getText().toString().equals("")) {
                    CommonUtil.showToast(mActivity, "请输入密码");
                    return;
                }
                Complete();
            }
        });

        tv_send_verificate_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_phone_number.getText().toString().equals("")) {
                    CommonUtil.showToast(mActivity, "请输入手机号");
                    return;
                }
                if (tv_send_verificate_code.getText().toString().equals("发送验证码")) {
                    VerificateCode();
                    countDownTimer.start();
                    tv_verificate_message.setVisibility(View.VISIBLE);
                } else {
                    return;
                }
            }
        });
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //获取短信验证码
    private void VerificateCode() {
        CustomProgressDialog.createDialog(mActivity).Show();
        final VerificateCodeReq req = new VerificateCodeReq();
        req.setMobile(et_phone_number.getText().toString());
        req.setType("resetpassword");
        Log.e("ssss", "短信验证码source==>" + req.getSecret());

        String url = Const.COMMON_ADDRESS + req.urlString();
        RequestParams params = new RequestParams();

        try {
            Rsa rsa = new Rsa();
            secret = rsa.encryptByPublic(req.getSecret());
            Log.e("ssss", "短信验证码source==>" + req.getSecret());
        } catch (Exception e) {
            e.printStackTrace();
        }
        params.put("secret", secret);
        Async_http_get.get(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                Log.e("ssss", "短信验证码==" + response);
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                if (errno.equals("000")) {
                    CommonUtil.showToast(mActivity, errmsg);

                } else {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }

    private void Complete() {
        CustomProgressDialog.createDialog(mActivity).show();
        ModifyPasswordReq req = new ModifyPasswordReq();

        req.setPassword(et_password.getText().toString());
        req.setOld_password(et_oldpassword.getText().toString());
        req.setVerification(et_verificate_code.getText().toString());


        RequestParams params = new RequestParams();

        params.put("uuid", myApplication.getDataEntity().getUuid());
        params.put("token", sharedPreferencesUtil.getAccessToken());

        params.put("secret", req.getSecret());
        Log.e("ssss", "uuid==>" + myApplication.getDataEntity().getUuid());
        Log.e("ssss", "token==>" + sharedPreferencesUtil.getAccessToken());
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.put(Const.COMMON_ADDRESS + req.urlString() + myApplication.getDataEntity().getUuid(), params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                Log.e("url", "complete_json==" + response);
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                if (errno.equals("000")) {
                    sharedPreferencesUtil.setPassword(et_password.getText().toString());
                    CommonUtil.showToast(mActivity, errmsg);
                    finish();
                } else {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Log.e("ssss", "statusCode==>" + statusCode);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }

    CountDownTimer countDownTimer = new CountDownTimer(120000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            tv_send_verificate_code.setText(millisUntilFinished / 1000 + "秒");
            tv_send_verificate_code.setBackgroundResource(R.drawable.right_angle_rectangle_bg_gray);
            tv_send_verificate_code.setTextColor(getResources().getColor(R.color.side_text));
        }

        @Override
        public void onFinish() {
            tv_send_verificate_code.setText("发送验证码");
            tv_send_verificate_code.setBackgroundResource(R.drawable.right_angle_rectangle_red_all);
            tv_send_verificate_code.setTextColor(getResources().getColor(R.color.ffffff));
        }
    };
}
